package drds.datasource.connection_pool.impl;

import drds.datasource.connection_pool.api.SqlException;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

@Slf4j
public final class PooledResultSet implements drds.datasource.connection_pool.api.ResultSet {
    @Setter
    @Getter
    private final PooledStatement pooledStatement;
    @Setter
    @Getter
    private final ResultSet resultSet;
    @Setter
    @Getter
    protected boolean closed = false;

    public PooledResultSet(PooledStatement pooledStatement, ResultSet resultSet) {
        this.pooledStatement = pooledStatement;
        this.resultSet = resultSet;
    }

    /**
     * 异常校验
     */
    protected SqlException checkException(SQLException e, String method) throws SqlException {
        return pooledStatement.checkException(e, method);
    }

    @Override
    public void close() throws SqlException {
        try {
            resultSet.close();
            this.closed = true;
        } catch (SQLException e) {
            throw checkException(e, "resultSet.close()");
        }
    }

    @Override
    public boolean isClosed() throws SqlException {
        return this.closed;
    }

    @Override
    public int getFetchSize() throws SqlException {
        try {
            return resultSet.getFetchSize();
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getFetchSize()");
        }
    }

    @Override
    public void setFetchSize(int rows) throws SqlException {
        try {
            resultSet.setFetchSize(rows);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.setFetchSize()");
        }
    }

    @Override
    public ResultSetMetaData getResultSetMetaData() throws SqlException {
        try {
            return resultSet.getMetaData();
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getMetaData()");
        }
    }

    @Override
    public int findColumn(String columnLabel) throws SqlException {
        try {
            return resultSet.findColumn(columnLabel);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.findColumn(columnLabel)");
        }
    }

    @Override
    public boolean next() throws SqlException {
        try {
            return resultSet.next();
        } catch (SQLException e) {
            throw checkException(e, "resultSet.next()");
        }
    }
    //


    @Override
    public String getString(int columnIndex) throws SqlException {
        try {
            return resultSet.getString(columnIndex);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getString(columnIndex)");
        }
    }

    @Override
    public String getString(String columnLabel) throws SqlException {
        try {
            return resultSet.getString(columnLabel);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getString(columnLabel)");
        }
    }

    @Override
    public BigDecimal getBigDecimal(int columnIndex) throws SqlException {
        try {
            return resultSet.getBigDecimal(columnIndex);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getBigDecimal(columnIndex)");
        }
    }

    @Override
    public BigDecimal getBigDecimal(String columnLabel) throws SqlException {
        try {
            return resultSet.getBigDecimal(columnLabel);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getBigDecimal(columnLabel)");
        }
    }

    //
    @Override
    public int getInt(int columnIndex) throws SqlException {
        try {
            return resultSet.getInt(columnIndex);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getInt(columnIndex)");
        }
    }

    @Override
    public int getInt(String columnLabel) throws SqlException {
        try {
            return resultSet.getInt(columnLabel);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getInt(columnLabel)");
        }
    }

    @Override
    public long getLong(int columnIndex) throws SqlException {
        try {
            return resultSet.getLong(columnIndex);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getLong(columnIndex)");
        }
    }

    @Override
    public long getLong(String columnLabel) throws SqlException {
        try {
            return resultSet.getLong(columnLabel);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getLong(columnLabel)");
        }
    }


    @Override
    public java.sql.Date getDate(int columnIndex) throws SqlException {
        try {
            return resultSet.getDate(columnIndex);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getDate(columnIndex)");
        }
    }

    @Override
    public java.sql.Date getDate(String columnLabel) throws SqlException {
        try {
            return resultSet.getDate(columnLabel);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getDate(columnLabel)");
        }
    }

    @Override
    public java.sql.Time getTime(int columnIndex) throws SqlException {
        try {
            return resultSet.getTime(columnIndex);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getTime(columnIndex)");
        }
    }

    @Override
    public java.sql.Time getTime(String columnLabel) throws SqlException {
        try {
            return resultSet.getTime(columnLabel);
        } catch (SQLException t) {
            throw checkException(t, "resultSet.getTime(columnLabel)");
        }
    }

    @Override
    public java.sql.Timestamp getTimestamp(int columnIndex) throws SqlException {
        try {
            return resultSet.getTimestamp(columnIndex);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getTimestamp(columnIndex)");
        }
    }


    @Override
    public java.sql.Timestamp getTimestamp(String columnLabel) throws SqlException {
        try {
            return resultSet.getTimestamp(columnLabel);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getTimestamp(columnLabel)");
        }
    }


    @Override
    public Object getObject(int columnIndex) throws SqlException {
        try {
            return resultSet.getObject(columnIndex);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getObject(columnIndex)");
        }
    }

    @Override
    public Object getObject(String columnLabel) throws SqlException {
        try {
            return resultSet.getObject(columnLabel);
        } catch (SQLException e) {
            throw checkException(e, "resultSet.getObject(columnLabel)");
        }
    }


}
